// Copyright (c) 2006 Zorm
// Released under the MIT License

#include "PCH.h"
#include "..\Common.h"

bool Window::RegisterClass() {
	WNDCLASSEX wcex;
	wcex.cbSize			= sizeof(WNDCLASSEX); 
	wcex.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wcex.lpfnWndProc	= Window::WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= m_pgmInstance;
	wcex.hIcon			= LoadIcon(m_pgmInstance, MAKEINTRESOURCE(ICON_ATH32));
	wcex.hIconSm		= LoadIcon(m_pgmInstance, MAKEINTRESOURCE(ICON_ATH16));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)COLOR_BACKGROUND;
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= ClassName();
	if (!RegisterClassEx(&wcex)) {
		return false;
	}
	return true;
}

LRESULT CALLBACK Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	Window *self;
	if (message == WM_NCCREATE) {
		LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
		self = reinterpret_cast<Window *>(lpcs->lpCreateParams);
		self->m_windowHWND = hWnd;
		SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(self));
	} else {
		self = reinterpret_cast<Window *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
	}

	if (self) {
		return self->HandleMessage(message, wParam, lParam);
	} else {
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
}
